<template>
<div>
  <div class="backgroundpic">
    <div style="height: 150px;"></div>
    <h2 style="color:red;margin-left: 49%;width: 120px;">欢 迎 注 册</h2>
    
    <el-form
    :model="form"
      status-icon
      :rules="rules"
      ref="form"
      label-width="100px"
      class="demo-ruleForm"
      style="width: 35%;margin: auto;"
    >
    <el-form-item label="账号" prop="phone">
        <el-input v-model.number="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="form.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-edit" @click="registers"
          >提交</el-button
        >
        <el-button @click="clean">重置</el-button>
        <el-button type="primary"  icon="el-icon-check"  @click="goLogin">去登录</el-button>
      </el-form-item>

</el-form>
</div>
</div>
</template>

<script>
import {register} from "@/api/register.js"
export default {
    data(){
        return{
            form:{
                phone: "",
                password: "",
                checkPass: "",
            },
        }
    },   
    methods:{
        //跳转到登录页面
        goLogin(){
            this.$router.push("/")
        },
        //注册
        registers(){
            register(this.form).then(res=>{
                if(this.form.phone!=""&&this.form.password!=""){
                    if(this.form.password==this.form.checkPass){
                        if(res.data.code==200){
                            this.$message({
                                    type:"success",
                                    message:"注册成功,去登录"
                                })
                            this.form.password=""    
                            this.form.checkPass=""   
                        }else{
                                alert("账户已存在")
                        }
                }else{
                    alert("两次密码不一致")
                }
                }else{
                    alert("账号密码不得为空")
                }
               
            })
            },
        clean(){
            this.form={}
        }    
        }
   
}
</script>

<style scoped>
.backgroundpic {
  width: 100%;
  height: 100ch;
  background-image: url("../assets/resgister.jpg");
  background-size: cover;
  background-position: center;
  position: relative;
}
</style>